package com.example.skillcinema.db.localdb.daos

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.example.skillcinema.db.localdb.tables.CollectionEntity
import com.example.skillcinema.db.localdb.tables.FilmEntity

@Dao
interface FilmDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFilm(film: FilmEntity)

    @Update
    suspend fun updateFilm(film: FilmEntity)

    @Delete
    suspend fun deleteFilm(film: FilmEntity)

    @Query("SELECT * FROM films WHERE film_id = :id")
    suspend fun getFilmById(id: Int): FilmEntity?

    @Query("SELECT * FROM films")
    suspend fun getAllFilms(): List<FilmEntity>

    @Query("""
        SELECT films.* FROM films 
        INNER JOIN film_collections ON films.film_id = film_collections.film_id
        INNER JOIN collections ON film_collections.collection_id = collections.collection_id
        WHERE collections.collection_type = :type
    """)
    suspend fun getFilmsByCollectionType(type: CollectionEntity.CollectionType): List<FilmEntity>

}